package com.qf.controller;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;

@RestController
@CrossOrigin
public class AudioController {
    private String path = "D:\\IdeaProjects\\SpringBoot\\GoGame\\src\\main\\resources\\upload\\uploadAudio\\";


    //上传音频
    @RequestMapping("/audio/upload")
    public String uploaderAudio(MultipartFile file) {
        String fileName = UUID.randomUUID().toString();
        try (
            InputStream in = file.getInputStream();
            OutputStream out = new FileOutputStream(path + fileName);
        ){
            IOUtils.copy(in, out);
        }catch(IOException e){
            e.printStackTrace();
        }
        return fileName;
    }

    //
    @RequestMapping("/audio/get")
    public void getAudio(String filename, HttpServletResponse response) {
        try(
                InputStream in = new FileInputStream(path + filename);
                ServletOutputStream out = response.getOutputStream();
        ) {
            IOUtils.copy(in,out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
